package com.noahplat.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.user.mapper.AvatarMapper;
import com.noahplat.user.pojo.Avatar;
import com.noahplat.user.service.AvatarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 头像操作接口
 *
 * @author 李东
 * @version 1.0.0
 * @Date: 2021/3/13 9:55
 **/
@Service
public class AvatarServiceImpl implements AvatarService {


    @Autowired
    private AvatarMapper avatarMapper;

    /**
     * 根据stid 查询头像列表
     *
     * @param stid
     * @return
     */
    @Override
    public Result<List<Avatar>> findAvatarList(Long stid) {
        Example example = new Example(Avatar.class);
        example.createCriteria().andEqualTo("stid", stid);
        List<Avatar> list = avatarMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(list)) {
            return new Result(true, StatusCode.NOTFOUNDERROR, "未查询头像信息！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", list);
    }

    @Override
    public Result<Avatar> findAvatarById(Long id) {
        Example example = new Example(Avatar.class);
        example.createCriteria().andEqualTo("id", id);
        Avatar avatar = avatarMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(avatar)) {
            return new Result(true, StatusCode.NOTFOUNDERROR, "未查询头像信息！");
        }
        return new Result(true, StatusCode.OK, "查询成功！", avatar);
    }

    /**
     * 添加头像信息
     * @param avatar
     * @return
     */
    @Override
    public Result addAvatar(Avatar avatar){
        avatar.setCreateTime(new Date());
        int res = avatarMapper.insertSelective(avatar);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改头像信息
     * @param avatar
     * @return
     */
    @Override
    public Result updateAvatar(Avatar avatar){
        int res = avatarMapper.updateByPrimaryKeySelective(avatar);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除头像信息
     * @param id
     * @return
     */
    @Override
    public Result delAvatar(Long id){
        int res = avatarMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 分页查询信息
     * @param page
     * @param limit
     * @return
     */
    @Override
    public Result getAvatars(Integer page,Integer limit){
        PageHelper.startPage(page,limit);
        List<Avatar> list = avatarMapper.getAvatar();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<Avatar> pageInfo = new PageInfo<>(list);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }
}
